﻿using System;
using System.Collections.Generic;

using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;

namespace KubertSoft.MSBuildTasks.Subversion
{
    /// <summary>
    /// Dictionary of the task items.
    /// </summary>
    public class TaskItemDictionary : Dictionary<string, ITaskItem>
    {

        /// <summary>
        /// Registers the output task item in the collection.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="item">The item.</param>
        public void RegisterTaskItem(string key, ITaskItem item)
        {
            this[key] = item;
        }

        /// <summary>
        /// Gets the task item fro output collection if exists or return a new one.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns>The task item that corresponds to key.</returns>
        public ITaskItem GetOptional(string key)
        {
            if (!ContainsKey(key))
            {
                this[key] = new TaskItem();
            }
            return this[key];
        }

        /// <summary>
        /// Gets the task item fro output collection if exists or return a new one.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns>The task item that corresponds to key.</returns>
        public ITaskItem GetRequired(string key)
        {
            if (!ContainsKey(key))
            {
                throw new ApplicationException("Required task item parameter is not exists.");
            }
            return this[key];
        }

        /// <summary>
        /// Logs the list of items.
        /// </summary>
        /// <param name="log">The task logging helper.</param>
        /// <param name="format">The format of line for one item.</param>
        public void LogItems(TaskLoggingHelper log, string format)
        {
            foreach (var item in this)
            {
                log.LogMessage(format, item.Key, item.Value.ToString());
            }
        }

    }
}
